// Project: fcounter2550LD
// FileName: fcounter2550LD
#include <p18cxxx.h>
#include <delays.h>
#include <timers.h>
#pragma config FOSC=HSPLL_HS, PLLDIV=5, CPUDIV=OSC1_PLL2
#pragma config USBDIV=2, PWRT=OFF, FCMEN=OFF, IESO=OFF
#pragma config BOR=OFF, BORV=2, WDT=OFF, LVP=OFF, VREGEN=OFF
#pragma config MCLRE=OFF, PBADEN=OFF, WDTPS=1024
#pragma config LPT1OSC=OFF, CCP2MX=OFF, DEBUG=OFF
#pragma config CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPB=OFF
#pragma config CPD=OFF, WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF
#pragma config WRTB=OFF, WRTC=OFF, WRTD=OFF, EBTR0=OFF
#pragma config EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF
#define lcd_DB LATB
#define lcd_E LATBbits.LATB3
#define lcd_RS LATBbits.LATB0
#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_50mS Delay10KTCYx(60)
#define Delay_100mS Delay10KTCYx(120)
// double ww, w;
unsigned long FFF, FF, F, B, FFFF;
unsigned long A, AA, TH, TL;
unsigned long aa, bb, cc, dd;
unsigned int N;
unsigned char sw, s;
char frq0[] = "f (MHz)";
char frq1[] = "f Low(kHz)";
char frqP[] = "fPrescaler(MHz)";
char Hz[] = "(MHz)";
char gps[] = "M (MHz) N=";
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_putf(unsigned long f);
void lcd_putfLow(unsigned long f);
void lcd_putfPre(unsigned long f);
void lcd_putfPre2(unsigned long f);
void lcd_putfPre3(unsigned long f);
void UserInit(void);
void lcd_Init(void);
void high(void);
void low(void);
#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code
void high(){
if(INTCONbits.TMR0IF){
A++;
INTCONbits.TMR0IF = 0;
}
}
void low(){
if(INTCON3bits.INT1IF){
Delay1TCY();
T0CONbits.TMR0ON = 0;
Delay1TCY();
TL = TMR0L; TH = TMR0H;
AA = A;
A = 0; TMR0H = 0; TMR0L = 0;
PORTCbits.RC0 = 0;
Delay1TCY();Delay1TCY();
PORTCbits.RC0 = 1;
PORTCbits.RC7 = 0;
Delay1TCY();Delay1TCY();
PORTCbits.RC7 = 1;
aa = 1; bb = 1; cc = 1; dd = 1;
if(PORTAbits.RA0) aa = 0;
if(PORTAbits.RA1) bb = 0;
if(PORTAbits.RA2) cc = 0;
if(PORTAbits.RA3) dd = 0;
if(sw == 0){
FFF = (AA*65536 + TH*256 + TL)*16 \
+ dd*8 + cc*4 + bb*2 + aa;
N = 0; B = 0; s = 0;
}
if(sw){
FFF = (AA*65536 + TH*256 + TL)*16 \
+ dd*8 + cc*4 + bb*2 + aa;
N++;
B = B + FFF;
if(N == 10){
FFF = B; s = 1; FFFF = B;
}
if(N > 10){
FFF = FFFF;
if(N == 100){
FFF = B; s = 2; FFFF = B;
}
if(N > 100){
FFF = FFFF;
}
}
}
INTCON3bits.INT1IF = 0;
PORTAbits.RA5 = 1;
Delay1TCY();
T0CONbits.TMR0ON = 1;
PORTAbits.RA5 = 0;
Delay1TCY();
}
if(INTCON3bits.INT2IF){
INTCON3bits.INT2IF = 0;
if(sw == 0){
sw = 1;
} else {
sw = 0;
}
Delay_20mS;
}
}
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(20);
lcd_E = 0;
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putf(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
lcd_putchr(buf[1]);
lcd_putchr(buf[2]);
lcd_putchr(buf[3]);
lcd_putchr('.');
for(i=4; i<10; i++) lcd_putchr(buf[i]);
}
void lcd_putg(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
lcd_putchr(buf[1]);
lcd_putchr(buf[2]);
lcd_putchr('.');
for(i=3; i<9; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
lcd_putchr(buf[9]);
}
void lcd_putgg(
unsigned long f){
unsigned char i;
unsigned char buf[11];
for(i=0; i<10; i++) buf[i] = ' ';
i = 10;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
lcd_putchr(buf[0]);
lcd_putchr(buf[1]);
lcd_putchr(buf[2]);
lcd_putchr('.');
for(i=3; i<9; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
lcd_putchr(buf[9]);
lcd_putchr(buf[10]);
}
void lcd_putui(
unsigned int ui, unsigned char d){
unsigned char i;
unsigned char buf[5];
for(i=0; i<10; i++) buf[i] = ' ';
i = 4;
do{
buf[i] = (ui % 10) + '0';
ui = ui / 10;
i--;
} while(ui > 0);
for(i=(5-d); i<5; i++) lcd_putchr(buf[i]);
}
void lcd_putfLow(
unsigned long f){
unsigned char i;
unsigned char buf[10];
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=2; i<7; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=7; i<10; i++) lcd_putchr(buf[i]);
}
void lcd_putfPre(
unsigned long f){
unsigned char i;
unsigned char buf[10];
f = f * 64;
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=0; i<4; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=4; i<9; i++) lcd_putchr(buf[i]);
}
void lcd_putfPre2(
unsigned long f){
unsigned char i;
unsigned char buf[10];
f = f * 128;
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=0; i<4; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=4; i<9; i++) lcd_putchr(buf[i]);
}
void lcd_putfPre3(
unsigned long f){
unsigned char i;
unsigned char buf[10];
f = f/10;
f = f * 512;
for(i=0; i<10; i++) buf[i] = ' ';
i = 9;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=0; i<5; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=5; i<9; i++) lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_50mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x01);
lcd_c4(0x06);
}
void UserInit(void){
FFF = 0; FF = 1;
TMR0H = 0; TMR0L = 0; A = 0;
PORTCbits.RC0 = 1;
PORTCbits.RC7 = 1;
sw = 0; s = 0;
N = 0; B = 0;
}
void main(void){
ADCON1 = 0x0F;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTE = 0x00;
TRISA = 0b00011111;
TRISB = 0b00000110;
TRISC = 0b01000110;
OpenTimer0(
TIMER_INT_ON &
T0_16BIT &
T0_PS_1_1 &
T0_SOURCE_EXT &
T0_EDGE_RISE
);
INTCON2bits.TMR0IP = 1;
INTCONbits.TMR0IF = 0;
INTCON3bits.INT1IE = 1;
INTCON3bits.INT1IP = 0;
INTCON3bits.INT1IF = 0;
INTCON3bits.INT2IE = 1;
INTCON3bits.INT2IP = 0;
INTCON3bits.INT2IF = 0;
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
UserInit();
lcd_init();
while(1){
// w = FFF;
// ww = w*1.0000004167;
// F = ww;
if(FF != FFF){
FF = FFF;
if(sw == 0){
if(PORTEbits.RE3 == 0){
lcd_clr();
Delay_1mS;//1行目先頭誤動作対応
lcd_locate(0, 1);
Delay_1mS;
lcd_puts(frq0);
lcd_locate(1, 4);
lcd_putf(FF);
}
if(PORTCbits.RC6 == 0){//不使用
lcd_clr();
lcd_locate(0, 1);
lcd_puts(frqP);
lcd_locate(1, 4);
lcd_putfPre(FF);
}
if(PORTCbits.RC1 == 0){
lcd_clr();
Delay_1mS;
lcd_locate(0, 1);
Delay_1mS;
lcd_puts(frqP);
lcd_locate(1, 4);
lcd_putfPre2(FF);
}
if(PORTCbits.RC2 == 0){
lcd_clr();
Delay_1mS;
lcd_locate(0, 1);
Delay_1mS;
lcd_puts(frqP);
lcd_locate(1, 4);
lcd_putfPre3(FF);
}
}
if(sw == 1){
if(PORTEbits.RE3 == 0){
if(s == 0){
lcd_clr();
Delay_1mS;
lcd_locate(0, 0);
Delay_1mS;
lcd_puts(gps);
lcd_putui(N, 4);
lcd_locate(1, 2);
lcd_putf(FF);
}
if(s == 1){
lcd_clr();
Delay_1mS;
lcd_locate(0, 0);
Delay_1mS;
lcd_puts(gps);
lcd_putui(N, 4);
lcd_locate(1, 2);
lcd_putg(FF);
}
if(s == 2){
lcd_clr();
Delay_1mS;
lcd_locate(0, 0);
Delay_1mS;
lcd_puts(gps);
lcd_putui(N, 4);
lcd_locate(1, 2);
lcd_putgg(FF);
}
}
}
}
Delay_10mS;
}
}